Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
For in loop on object, and .find, returning "undefined"

I'm trying to loop through an object (objectToLoop) and check to see if if contains the key comments_text. If it does, I want to check comments_text, and if the key within comments_text matches commentId, I want to set let item to the entire object within coments_text.

I'm trying to set let item to the following, but I'm getting "undefined":

{
 commenter_comment: "reedwe",
 commenter_handle: "FALCON1",
 commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
 key: "-MlaKXBMXzXIBJMsZQNX"
}

Advice and feedback appreciated.

const commentId = "-MlaKXBMXzXIBJMsZQNX"

const objectToLoop = {
  comment: false,
  comments_text: {
    commenter_comment: "reedwe",
    commenter_handle: "FALCON1",
    commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
    key: "-MlaKXBMXzXIBJMsZQNX"
  },
  handle: "TURTLE1",
  key: "-MkeOOUboKdOcC3Sannl",
  title: "hello2"
}

function returnMatchingComment(commentIdKey) {
  let item;
  for (const post in objectToLoop) {
    if (post.comments_text) {
      item = Object.values(post.comments_text).find(
        (comment) => comment.key === commentIdKey
      )
    };
    if (item) {
      break;
    }
  }
  return item
}
console.log(
  returnMatchingComment(commentId)
) 

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You do not need to use the for...in loop. You can first check if objectToLoop has the property comments_text by using Object.prototype.hasOwnProperty() and check if objectToLoop?.comments_text?.key == commentId. I used ?. (optional chaining) so that if objectToLoop.comments_text didn't exist, it would just return undefined and not throw an ugly error.
Example:

const commentId = "-MlaKXBMXzXIBJMsZQNX"

const objectToLoop = {
  comment: false,
  comments_text: {
    commenter_comment: "reedwe",
    commenter_handle: "FALCON1",
    commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
    key: "-MlaKXBMXzXIBJMsZQNX"
  },
  handle: "TURTLE1",
  key: "-MkeOOUboKdOcC3Sannl",
  title: "hello2"
}
if(objectToLoop.hasOwnProperty('comments_text') && objectToLoop?.comments_text?.key == commentId) { 
    let item = objectToLoop.comments_text;
    console.log(item)
}
To be honest, we don't even need objectToLoop.hasOwnProperty('comments_text') as with optional chaining (as mentioned earlier on), objectToLoop?.comments_text?.key == commentId would just return undefined if objectToLoop.comments_text did not exist or false if objectToLoop.comments_text.key was not equal to commentId.
Example 2:

const commentId = "-MlaKXBMXzXIBJMsZQNX"

const objectToLoop = {
  comment: false,
  comments_text: {
    commenter_comment: "reedwe",
    commenter_handle: "FALCON1",
    commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
    key: "-MlaKXBMXzXIBJMsZQNX"
  },
  handle: "TURTLE1",
  key: "-MkeOOUboKdOcC3Sannl",
  title: "hello2"
}

if(objectToLoop?.comments_text?.key == commentId) { 
    let item = objectToLoop.comments_text;
    console.log(item)
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You can put some debug.Your if never fire. The 'i' log gives you a big information =)

const commentId = "-MlaKXBMXzXIBJMsZQNX"

const objectToLoop = {
  comment: false,
  comments_text: {
    commenter_comment: "reedwe",
    commenter_handle: "FALCON1",
    commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
    key: "-MlaKXBMXzXIBJMsZQNX"
  },
  handle: "TURTLE1",
  key: "-MkeOOUboKdOcC3Sannl",
  title: "hello2"
}

function returnMatchingComment(commentIdKey) {
  let item;
  var i =0;
  for (const post in objectToLoop) {
    console.log(post,i++);
    if (post.comments_text) {
      console.log("APPEND");
      item = Object.values(post.comments_text).find(
        (comment) => comment.key === commentIdKey
      )
    };
    if (item) {
      break;
    }
  }
  return item
}
console.log(
  returnMatchingComment(commentId)
) 

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda